package framework.logging;

import java.util.List;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.aspectj.lang.JoinPoint;

import framework.constants.CONSTANTS;

public class SpringLogger implements ISpringLogger{
  private static Logger logger = LogManager.getLogger(CONSTANTS.LoggerName);
  private final String MYLOG = "<<LOGGER>> ";

  public SpringLogger(){
  }

  public void log(){
    logger.fatal("doLog");
  }

  private void myLog(String message){
    logger.fatal(MYLOG + message);  
  }
  
  public void logBefore(JoinPoint jp) {
    myLog("Calling " + jp.getSignature() + " with params : " + getJoinPointArgs(jp));  
  }

  public void logAfter(JoinPoint jp) {
    myLog("doLogAfter = " + jp.getArgs()[0]);
  }

  public void logAfterReturn(JoinPoint jp,List<?> list){
    myLog("List.size = " + list.size() + "; List.items = " + getJoinPointArgs(jp));
  }
  
  private String getJoinPointArgs(JoinPoint jp){
    StringBuilder params = new StringBuilder();
    Object[] args = jp.getArgs();
    for (int i = 0; i < args.length; i++)
      params.append(args[i] + "; ");
    return params.toString();
  }

}
